/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Boundary
import com.*
import javax.swing.*
import java.io.* 
import com.itextpdf.*
import java.awt.Desktop;
import java.io.File;
import com.itextpdf.text.pdf.CFFFont.Font
import com.itextpdf.text.Paragraph
import com.itextpdf.text.pdf.PdfPCell
import com.itextpdf.text.pdf.PdfPTable
import com.itextpdf.text.pdf.PdfWriter
import com.itextpdf.text.Document
import com.itextpdf.text.Font
import control.*
import entity.*
/**
 *
 * @author JAVIER
 */
class Report {
   //CReate of report 
   public Report(OrderControl control,Order order,WoodCatalog catalog,String rut){
      newReport(control,order,catalog,rut)
   }
    
   public void newReport(OrderControl control,Order order,WoodCatalog catalog,String rut){
       //font tipes of text
      Font Fac = new Font(Font.getFamily("ARIAL"), 30, Font.BOLD);
      Font Title = new Font(Font.getFamily("ARIAL"), 13, Font.BOLD);
      //assign file path
      FileOutputStream file = new FileOutputStream(rut);
      Document document = new Document();
      PdfWriter.getInstance(document, file);
      document.setMargins(72f, 72f, 72f, 72f);
      document.open();
      String fact1 = "FACTURA\n\n\n\n";
      Paragraph Fact = new Paragraph(fact1,Fac);
      document.add(Fact);
      PdfPTable table=new PdfPTable(3); 
      table.setWidthPercentage(100);
      PdfPCell type_table =new PdfPCell (new Paragraph("MADERA",Title));;
      PdfPCell amount_table =new PdfPCell (new Paragraph("CANTIDAD",Title));;
      PdfPCell price_table =new PdfPCell (new Paragraph("PRECIO",Title));;
      table.addCell(type_table);
      table.addCell(amount_table);
      table.addCell(price_table);
      //fill table
      if (order && order.woods) {
         order.woods.each {
            if (it.value > 0){
            table.addCell(catalog.woodNames.get(it.key));
            table.addCell(it.value.toString());
            table.addCell((it.value*catalog.bfPrices.get(it.key)).toString());
            }
         };
      }
      //fill fields  
      table.addCell(" ");
      table.addCell("PRECIO TOTAL");
      table.addCell( control.totalPrice(order).toString());
      
      String name = "...      ";
      if (order.clientName != null) 
         name  =  order.clientName;
      
      String address  = "...     "; 
      if (order.deliveryAddress != null) 
         address  = order.deliveryAddress;
      //fill report
      document.add(new Paragraph("NOMBRE:\n"));
      document.add(new Paragraph(name+"\n\n"));
      document.add(new Paragraph("DIRECCION DE ENVIO:\n"));
      document.add(new Paragraph(address+"\n\n"));
      document.add(new Paragraph("TIEMPO ESTIMADO DE ENVIO:\n"));
      document.add(new Paragraph(control.totalDeliveryTime(order).toString()+"\n\n"));
      

      document.add(table);
      //open the report in the pc
      document.close();
      try {
      File pdf = new File(rut);
        Desktop.getDesktop().open(pdf);
      } catch(Exception e) {
        e.printStackTrace();
      }
   }	
}